/// 收藏记录模型 - 用于本地SQLite存储
/// Favorite Record Model - For local SQLite storage
class Favorite {
  final int? id;
  final String wikiId;
  final String wikiName;
  final String wikiUrl;
  final String title;
  final String description;
  final String iconUrl;
  final DateTime createdAt;
  final DateTime? updatedAt;

  Favorite({
    this.id,
    required this.wikiId,
    required this.wikiName,
    required this.wikiUrl,
    required this.title,
    this.description = '',
    this.iconUrl = '',
    required this.createdAt,
    this.updatedAt,
  });

  factory Favorite.fromJson(Map<String, dynamic> json) {
    return Favorite(
      id: json['id'] as int?,
      wikiId: json['wikiId'] ?? '',
      wikiName: json['wikiName'] ?? '',
      wikiUrl: json['wikiUrl'] ?? '',
      title: json['title'] ?? '',
      description: json['description'] ?? '',
      iconUrl: json['iconUrl'] ?? '',
      createdAt: DateTime.parse(json['createdAt'] ?? DateTime.now().toIso8601String()),
      updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'wikiId': wikiId,
      'wikiName': wikiName,
      'wikiUrl': wikiUrl,
      'title': title,
      'description': description,
      'iconUrl': iconUrl,
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt?.toIso8601String(),
    };
  }

  /// 转换为数据库映射 Convert to database map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'wiki_id': wikiId,
      'wiki_name': wikiName,
      'wiki_url': wikiUrl,
      'title': title,
      'description': description,
      'icon_url': iconUrl,
      'created_at': createdAt.millisecondsSinceEpoch,
      'updated_at': updatedAt?.millisecondsSinceEpoch,
    };
  }

  /// 从数据库映射创建 Create from database map
  factory Favorite.fromMap(Map<String, dynamic> map) {
    return Favorite(
      id: map['id'] as int?,
      wikiId: map['wiki_id'] ?? '',
      wikiName: map['wiki_name'] ?? '',
      wikiUrl: map['wiki_url'] ?? '',
      title: map['title'] ?? '',
      description: map['description'] ?? '',
      iconUrl: map['icon_url'] ?? '',
      createdAt: DateTime.fromMillisecondsSinceEpoch(map['created_at'] ?? 0),
      updatedAt: map['updated_at'] != null 
          ? DateTime.fromMillisecondsSinceEpoch(map['updated_at']) 
          : null,
    );
  }

  Favorite copyWith({
    int? id,
    String? wikiId,
    String? wikiName,
    String? wikiUrl,
    String? title,
    String? description,
    String? iconUrl,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return Favorite(
      id: id ?? this.id,
      wikiId: wikiId ?? this.wikiId,
      wikiName: wikiName ?? this.wikiName,
      wikiUrl: wikiUrl ?? this.wikiUrl,
      title: title ?? this.title,
      description: description ?? this.description,
      iconUrl: iconUrl ?? this.iconUrl,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  String toString() {
    return 'Favorite{id: $id, wikiId: $wikiId, title: $title, createdAt: $createdAt}';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Favorite &&
          runtimeType == other.runtimeType &&
          wikiId == other.wikiId &&
          wikiUrl == other.wikiUrl;

  @override
  int get hashCode => wikiId.hashCode ^ wikiUrl.hashCode;
}